/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package easybooking;

import easybooking.db.DBConnection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Flight Server Controller Class which organizes GUI and application logic
 * @author Tim Schröder
 */
public class FlightServerController implements ActionListener {

    private FlightServer server;
    private static FlightServerGui fsg;
    private static FlightServerController fsc;

    /**
     * Main method to start the server.
     * @param args
     */
    public static void main(String args[]) {
        fsc = new FlightServerController();
        fsg = new FlightServerGui();
        fsg.setTextFields("localhost", DBConnection.getDbHost(), DBConnection.getDbName(), DBConnection.getDbUser(), DBConnection.getDbPassword());
        fsg.createAndShowGUI(fsc);
    }

    /**
     * Method to start the server.
     */
    private void startServer() {
        if (server == null) {
            DBConnection.setDbHost(fsg.getDbHost());
            DBConnection.setDbName(fsg.getDbName());
            DBConnection.setDbUser(fsg.getDbUser());
            DBConnection.setDbPassword(fsg.getDbPassword());
            server = new FlightServer(this);
            server.setRmiHost(fsg.getRmiHost());
            if (DBConnection.connection() != null) {
                fsg.getTextArea().append("Server started...\n");
                try {
                    String localhost = InetAddress.getLocalHost().getHostName();
                    for (InetAddress ia : InetAddress.getAllByName(localhost)) {
                        fsg.getTextArea().append("Server IP: " + ia + "\n");
                    }
                } catch (UnknownHostException ex) {
                    Logger.getLogger(FlightServerController.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else {
                fsg.getTextArea().append("Connection to database failed!...\n");
                this.stopServer();
            }

        } else {
            fsg.getTextArea().append("Server already started!\n");
        }
    }

    /**
     * Method to stop the server.
     */
    private void stopServer() {
        if (server != null) {
            server.finalize();
            server = null;
            DBConnection.unsetDBConnection();
            fsg.getTextArea().append("Server stopped...\n");

        } else {
            fsg.getTextArea().append("Server already stopped!\n");
        }
    }

    /**
     * Returns the gui of the server
     * @return
     */
    public FlightServerGui getServerGui() {
        return fsg;
    }

    /**
     *Processes the servers gui events.
     * @param e the event object
     */
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("startServer")) {
            this.startServer();
        }
        if (e.getActionCommand().equals("stopServer")) {
            this.stopServer();
        }
    }
}
